<%@ page import="org.apache.naming.java.javaURLContextFactory"%>
<%@page import="java.util.Date,java.util.List,com.rbcs.reimb.model.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<html>
<head>

<link rel="stylesheet" type="text/css" href="${ctx}/styles/ec/ecside_style.css" />
<%@include file="/common/extjs.jsp" %>
<%@include file="/common/meta.jsp"%>
<%@include file="/common/validator.jsp"%>
<%@ page import="com.rbcs.RbcsConstants" %>
<title>报销申请管理</title>
</head>
<body>
<div class="x-panel" style="width: 100%">
<div class="x-panel-header">报销申请管理</div>
<div><%@ include file="/common/messages.jsp"%></div>
<div align="center" style="width: 100%">
<s:form action="save.do" id="save" method="post">
	<s:hidden name="model.id" />

	<table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-left-width:0px; border-top-width:0px; padding: 4px 2px; margin-top: 10px;">
		<tr height="30" style="padding: 8px 2px;"> 
           <td align="center" colspan="8"><font size="3">报销单</font></td>
        </tr>	
        <tr height="30">
           <td align="right" width="180">报销单号：</td>
           <td align="left" colspan="2" >
           		<s:textfield  id="singleNo" name="model.singleNo" cssClass="required" cssStyle="width:135px; border:1px dotted #808080;"></s:textfield >
           		<font color="red">*</font>
			</td>
           <td align="right" width="130">部门名称：</td>
           <td align="left" colspan="1">&nbsp;${model.dept.name}</td>	 			
           <td align="right" width="120">制单日期：</td>
           <td align="left" colspan="2">&nbsp;<s:date name="model.createDate" format="yyyy-MM-dd HH:mm"/></td>		
        </tr>         
 
         <tr height="30">
           <td align="right" width="130">项目类别：</td>
           <td align="left" colspan="2">
           	  <select id="typeId" name="projectTypeId" style="width: 135px;">
           	     <option value="0">&nbsp;请选择...</option>
			     <s:iterator value="projectTypes" var="type">
			       <s:if test="model.project.projectType.id == #type.id">
			         <option value="${type.id}" selected="selected">&nbsp;${type.code}&nbsp;-&nbsp;${type.name}&nbsp;&nbsp;&nbsp;</option>
			       </s:if>
			       <s:else>
			       	 <option value="${type.id}">&nbsp;${type.code}&nbsp;-&nbsp;${type.name}&nbsp;&nbsp;&nbsp;</option>
			       </s:else>
			     </s:iterator>
			   </select>
			</td>
           <td align="right" width="120">项目名称：</td>
           <td align="center" colspan="4">
                <input type="hidden" id="projectId" name="projectId" value="${model.project.id}">
                <input type="text" id="projectName" value="${model.project.name}" onclick="openSelect()" style="width:415px; border:1px dotted #808080;">
           </td>	 				
        </tr>  
               
        <tr height="30">
           <td align="right" width="140">销账金额：</td>
           <td align="center" width="100">

                <s:textfield id="borrowAmount" name="model.borrowAmount" cssStyle="width:94px;border:1px dotted #808080;" readonly="true"/>
			</td>
           <td align="right" width="150">销账借款单号：</td>
           <td align="center" >
                <s:textfield id="totalBorrowNos" name="model.totalBorrowNos" onclick="openSelectborrows('total')" cssStyle="width:120px;border:1px dotted #808080;" readonly="true" title="单击此处选择完全销账的借款单"/>
                <s:hidden id="totalBorrowIds" name="model.totalBorrowIds" />
                <s:hidden id="totalBorrowAmounts"/>
                <s:hidden id="totalRepayAmounts"/>
                <s:hidden id="totalNoRepayAmounts" name="model.totalNoRepayAmounts"/>
			</td>
           <td align="center" width="150">部分销账借款单号：</td>
           <td align="center">
                <s:textfield id="partBorrowNos" name="model.partBorrowNos" onclick="openSelectborrows('part')" cssStyle="width:118px;border:1px dotted #808080;" readonly="true" title="单击此处选择部分销账的借款单"/>
                <s:hidden id="partBorrowIds" name="model.partBorrowIds"/>
                <s:hidden id="partBorrowAmounts"/>
                <s:hidden id="partRepayAmounts"/>
                <s:hidden id="partNoRepayAmounts"/>              
			</td>
           <td align="center" width="70">余款：</td>
           <td align="center" width="78">
                <s:textfield id="balance" name="model.balance" cssStyle="width:75px;border:1px dotted #808080;" readonly="true"/>
                <s:hidden id="offsetAmount" name="model.offsetAmount"/>  
			</td>						
        </tr>	    	
	</table>

	<table align="center" width="96%"> 
	<tr><td><div align="left"><input name="button" type="button" value="添加明细" class="button"  onclick="addRow()" style="width: 70px;"></div></td></tr>
	</table>
	
<table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-left-width:0px; border-top-width:0px; padding: 1px 1px; ">
	  <tbody id="tbody">
	    <tr bgcolor="#F3F4F3" align="center" height="30">
	       <td style="width:180px;">科目名称</td>
	       <td style="width:260px;">摘要</td>
	       <td style="width:100px;">金额</td>
	       <td style="width:100px;">外币金额</td>	
	       <td style="width:100px;">外币币种</td>	   	              	       	       
	       <td style="width:80px;">操作</td>
	    </tr>
	    <tr style="display:none" bgcolor="#FFFFFF"> 
	      <td align="center"><input name="itemName" type="text" onclick="checkSelectItem(this.parentNode.parentNode)" style="width:176px;border:1px dotted #808080;" title="请您选择科目"/>
	      <input type="hidden" name="itemId" id="itemId"/></td>
	      <td align="center"><input name="title" type="text" style="width:256px;border:1px dotted #808080;" title="请您填写摘要"/></td>
	      <td align="center"><input name="amount" type="text" onkeyup="yzsz(this, this.parentNode.parentNode)" style="width:96px;border:1px dotted #808080;" title="请您输入报销金额"/></td>
	      <td align="center"><input name="exchangeAmount" type="text" onkeyup="yzwbsz(this, this.parentNode.parentNode)" style="width:96px;border:1px dotted #808080;" title="请您输入报销外币金额"/></td>	      	      
	      <td align="center">
	      <input type="hidden" id="cRate"/>				  
	      <input name="cName" type="text" style="width:96px;border:1px dotted #808080;" onclick="selectCurrencys(this.parentNode.parentNode)" title="请您选择外币币种种类" />
	      <input type="hidden" id="cId" name="cId" />
	      </td> 	 
	      <td align="center"><input type="button" value="删除" onclick="removeRow(this.parentNode.parentNode)" class="button" ></td>
	    </tr>
	    
	   <s:if test="reimbDetail.size==0">
	   </s:if>
	   <%
	   List<ReimbDetail> reimbDetails = (List<ReimbDetail>)request.getAttribute("reimbDetail");
	   if(reimbDetails != null){
	   for(ReimbDetail rd : reimbDetails){
	   %>
	   <tr height="30" bgcolor="#FFFFFF"> 
	      <td align="center"><input name="itemName" type="text" value="<%=rd.getItem().getName()%>【<%=rd.getItem().getCode()%>】" onclick="checkSelectItem(this.parentNode.parentNode)" style="width:176px;border:1px dotted #808080;" title="请您选择科目"/>
	      <input type="hidden" name="itemId" value="<%=rd.getItem().getId()%>"/></td>	   
	      <td align="center"><input name="title" type="text" value="<%=rd.getTitle()%>" style="width:256px;border:1px dotted #808080;" title="请您填写摘要"/></td>
	      <td align="center"><input name="amount" value="<%=rd.getAmount()%>" type="text" onkeyup="yzsz(this)" style="width:96px;border:1px dotted #808080;" title="请您输入报销金额"/></td>
	      <td align="center"><input name="exchangeAmount" value="<%=rd.getExchangeAmount()%>" type="text" onkeyup="yzwbsz(this, this.parentNode.parentNode)" style="width:96px;border:1px dotted #808080;" title="请您输入报销外币金额"/></td>
	      <td align="center">
	      <c:set var="currency" value="<%=rd.getCurrency()%>" scope="request"/>
	      <s:if test="#attr.currency != null">
	         <input name="cName" type="text" value="<%=rd.getCurrency().getName()%>" style="width:96px;border:1px dotted #808080;" onclick="selectCurrencys(this.parentNode.parentNode)" title="请您选择外币币种种类" />
	         <input type="hidden" name="cId" id="cId" value="<%=rd.getCurrency().getId()%>"/>
	      </s:if>
	      <s:else>
		     <input name="cName" type="text" style="width:96px;border:1px dotted #808080;" onclick="selectCurrencys(this.parentNode.parentNode)" title="请您选择外币币种种类" />
	         <input type="hidden" name="cId" id="cId"/>      
	      </s:else>
	      </td> 	      	      	      
	      <td align="center"><input type="button" value="删除" onclick="removeRow(this.parentNode.parentNode)" class="button" ></td>	       
	    </tr>
	    <%}} %>	    	     
	    </tbody>
		
		<tr bgcolor="#FFFFFF" height="30"> 
	       <td colspan="2" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	       <input type="text" value="报销金额合计："  readonly="readonly" style="width:100px;padding-top: 5px;border: 0;"/>
	       <td align="left"><input id="reimbTotal" name="model.amount" type="text" readonly="readonly" style="width:96px;padding-top: 5px;border: 0;"/></td>
	       <td align="left">&nbsp;</td>	       	       	       
	       <td align="left">&nbsp;</td>
	       <td align="left">&nbsp;</td>
	    </tr> 
	</table>
    <br>
	<table width="600px" style="margin-bottom: 10px;">
		<tr>
			<td style="text-align: center;">
			  <s:submit value="保存" cssClass="button" onclick="return saveyz();"/> 
			  &nbsp;&nbsp;
			  <input class="button" type="button" value="重置" onclick="reset1()">
			  &nbsp;&nbsp;
			  <s:reset value="返回" cssClass="button" onclick="history.go(-1)"/>
			</td>
		</tr>
	</table>
</s:form>
</div>
</div>
<script type="text/javascript" src="${ctx }/pages/rbcs/reimb/editjs.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$("#save").validate();
	zjs(); 
});

/**
 * 根据项目类别选择项目
 */
function openSelect() {
	var typeId = document.getElementById("typeId").value ;
	if (typeId > 0 ) {
	   var url = '${ctx}/base/project/selectGroupProject.do?projectTypeId=' + typeId;		
	} else {
	   var url = '${ctx}/base/project/selectGroupProject.do';	
	}
	var styles = "dialogWidth:502px; dialogHeight:365px; status:no; scroll:no; help:no";
	var project = window.showModalDialog(url, null, styles);
	if (project != null){
    	document.getElementById("projectId").value = project.id;
    	document.getElementById("projectName").value = project.name;
	} else{
    	document.getElementById("projectId").value = "";
    	document.getElementById("projectName").value = "";
	}
	var tab = document.getElementsByName("itemName") ;
	for(var i = 1; i < tab.length; i++){
  	  tab[i].value = "";
  	}
}

/**
 * 判断是部门科目还是项目科目
 */
function checkSelectItem(r) {
	var projectId = document.getElementById("projectId").value;
	var deptId = '${model.dept.id}';
	if (projectId != null && projectId != "") {
		openSelectItem(r, projectId, "project");
	}else {
		openSelectItem(r, deptId, "dept");
	}
}

/**
 * 选择科目
 */
function openSelectItem(r, id, type){
	var url = null;
	if(type == "dept"){
		url = '${ctx}/base/item/selectItem.do?dept.id=' + id + '&inputType=radio';
	}else{
		url = '${ctx}/base/item/selectItem.do?project.id=' + id + '&inputType=radio';
	}
	var styles = "dialogWidth:630px; dialogHeight:400px; status:no; scroll:no; help:no";
	var item = window.showModalDialog(url, null, styles);
	if (!item){
		//do nothing...
	} else{
		var tab1 = document.getElementsByName("itemId") ;
		tab1[r.rowIndex-1].value = item.id;		
		var tab2 = document.getElementsByName("itemName") ;
		tab2[r.rowIndex-1].value = item.name + "【" + item.code + "】";
	
		if(item.name == '<%=RbcsConstants.ITEM_NAME_TRAVEL%>') {
		   var tab3 = document.getElementsByName("title");
		   var url = '${ctx}/base/tripgrade/selectTripGrade.do';
		   var styles = "dialogWidth:502px; dialogHeight:385px; status:no; scroll:no; help:no";
		   var tripGrade = window.showModalDialog(url, null, styles);		   
		   tab3[r.rowIndex-1].value = tripGrade.name + "【" + tripGrade.subsidy + "元/天】";
		}
	}
}
 
 /**
  * 点击保存以前的验证
  */
 function saveyz(){
 	 
 var tab = document.getElementsByName("itemName") ;
 var tab1 = document.getElementsByName("amount") ;
 if(tab.length == 1){
	alert("科目不能为空，请选择科目！");
	return false;
  }
 for(var i=1; i<tab.length; i++){
    if(tab[i] == null || tab[i].value == ""){
	   alert("科目不能为空，请选择科目！");
	   return false;
	} 
  }
 for(var i=1; i<tab1.length; i++){
	if(tab1[i].value == "" || tab1[i].value - 0 == 0 ){
	   alert("请输入金额，金额不能为0！");
	   return false;
	}
  }
 
 if(confirm('您确定生成报销申请信息吗？')){
	  return true;
   } else {
      return false;
   }   
 }
 
 /**
  * 重置销账借款单号
  */
 function reset1() {
   $("#totalBorrowNos").val("");
   $("#totalBorrowIds").val(""); 
   $("#totalBorrowAmounts").val(""); 
   $("#totalRepayAmounts").val(""); 
   $("#totalNoRepayAmounts").val("");
   
   $("#partBorrowNos").val("");
   $("#partBorrowIds").val(""); 
   $("#partBorrowAmounts").val(""); 
   $("#partRepayAmounts").val(""); 
   $("#partNoRepayAmounts").val("");
   $("#borrowAmount").val("");
   zjs(); 
 } 
</script>
</body>
</html>